package kku.cs.fgl;

import java.io.IOException;

import org.lwjgl.Sys;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import kku.cs.fgl.actor.ImageActor;

public class FooglIntroScene extends AbstractScene {
	protected GameLoader applet;

	public FooglIntroScene(GamePane gamePane) {
		super(-1, gamePane);
	}

	public void enter() {

	}

	ImageActor a[] = new ImageActor[6];

	int h = 600, w = 800;

	public void init() {
		h = screen.getHeight();
		w = screen.getWidth();
		try {
			a[0] = new ImageActor(100f, 500f, 0f, 0f, "logo/opengl.png");
			a[1] = new ImageActor(220f, 500f, 0f, 0f, "logo/openal.png");
			a[2] = new ImageActor(320f, 500f, 0f, 0f, "logo/lwjgl.png");
			a[3] = new ImageActor(400f, 500f, 0f, 0f, "logo/slick.png");
			a[4] = new ImageActor(400f, 500f, 0f, 0f, "logo/foogl.png");
			try {
				Image logo = applet.getLogo();
				if (logo == null)
					logo = new Image("logo/foogl.png");
				a[5] = new ImageActor(400f, 500f, 0f, 0f,logo);
			} catch (SlickException e) {
			}

			a[0].setX(w - a[0].getWidth());
			for (int i = 0; i < a.length; i++) {
				if (i != 5) {
					a[i].setScale(0.6f, 0.6f);
					if (i > 0) {
						a[i].setX(a[i - 1].getX() - a[i].getWidth() * 0.6f - 5);
					}
					a[i].setY(h - 60);
				}
				a[i].setAlphaLevel(0);
				add(a[i]);
			}
			// a[4].setScale(2f, 2f);
			a[5].setCenter(screen.getWidth() / 2, 200);
		} catch (IOException e) {
			e.printStackTrace();
		}
		screen.setShowFPS(false);

	}

	public void leave() {

	}

	public void paint(Graphics g) {
		g.setColor(new Color(0.5f, 0.5f, 0.5f, alpha));
		g.fillRect(0, 0, screen.getWidth(), h - 70);
		g.setColor(new Color(1f, 1f, 1f, alpha));
		g.fillRect(0, h - 70, screen.getWidth(), 70);
		Color b = new Color(50, 100, 200);
		getFont(0)
				.drawString(
						5,
						h - 70,
						FooGameLib.fullname + "\n" + FooGameLib.name
								+ " version " + FooGameLib.version + "\nLWJGL "
								+ Sys.getVersion(), b);
		getFont(0).drawString(5, h - 20,
				FooGameLib.copyright + " " + FooGameLib.url, b);
		super.paint(g);
	}

	int time = 0;

	float alpha = 1;

	public void update(int delta) {
		time += delta;
		screen.setShowFPS(false);
		if (time <= 550) {
			for (int i = 0; i < a.length; i++) {
				alpha = time / 500f;
				a[i].setAlphaLevel(alpha);
			}
			if (time >= 500) {
				for (int i = 0; i < a.length; i++) {
					a[i].setAlphaLevel(1);
				}
			}
		}
		if (time > 5000) {
			applet.initScenes(screen, gamePane);
			enterGameScene();
			time = 0;
		}
	}

	private void enterGameScene() {
		if (gamePane.getCurrentState() != this) {
			try {
				screen.setShowFPS(true);
				gamePane.getCurrentState().init(screen, gamePane);
				gamePane.getCurrentState().enter(screen, gamePane);
			} catch (SlickException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void keyPressed(int key, char c) {
		if (key != Input.KEY_SYSRQ && key != Input.KEY_F11)
			time = 5000;
	}

	@Override
	public void mouseClicked(int button, int x, int y, int clickCount) {
		time = 5000;
	}

}
